/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPnlSegments.java
 *
 * Created on 18/03/2010, 17:50:07
 */
package systole.view.tabs.segments;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import org.jdesktop.application.Action;
import org.jfree.chart.ChartPanel;
import systole.domain.signals.Segment;
import systole.utils.ImageUtils;
import systole.view.charts.XYChart;
import systole.view.imageFlow.GradientPanel;
import systole.view.imageFlow.ImageFlow;
import systole.view.imageFlow.StackLayout;
import systole.view.tabs.JPnlSegments;
import systole.view.tabs.controllers.AnalysisController;
import systole.view.tabs.controllers.SelectionItem;
import systole.view.tabs.controllers.SelectionModel;

/**
 * 
 * @author jmj
 */
public class JPnlImagesFlow extends javax.swing.JPanel implements Observer {

    /**
     *
     */
    private static final long serialVersionUID = -6806080186223038052L;
    private ImageFlow imageFlow = null;
    private AnalysisController analysisControl;
    private JPnlSegments parent;
    private JPnlItemFlow selectedToDelete = null;
    private HashMap<Integer, JPnlItemFlow> items;
    private int x = 0;
    private int y = 0;

    /**
     * Creates new form JPnlSegments
     *
     * @param logic
     * @param parent
     */
    public JPnlImagesFlow(AnalysisController logic, JPnlSegments parent) {
        initComponents();
        this.analysisControl = logic;
        this.parent = parent;
        this.items = new HashMap<Integer, JPnlItemFlow>();

        this.jBtnLast.setIcon(ImageUtils.buildIcon(this,
                "resources/navbar/arrow-left.png"));
        this.jBtnAdd.setIcon(ImageUtils.buildIcon(this,
                "resources/navbar/list-add.png"));
        this.jBtnQuit.setIcon(ImageUtils.buildIcon(this,
                "resources/navbar/list-remove.png"));
        this.jBtnNext.setIcon(ImageUtils.buildIcon(this,
                "resources/navbar/arrow-right.png"));
        this.jScrPnlCandidates.getHorizontalScrollBar().setUnitIncrement(20);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPnlFlowCharts = new javax.swing.JPanel();
		jPnlTopCenter = new javax.swing.JPanel();
		jPnlTopTop = new javax.swing.JPanel();
		jPnlTopBottom = new javax.swing.JPanel();
		jPnlTopRigth = new javax.swing.JPanel();
		jPnlTopLeft = new javax.swing.JPanel();
		jPnlSelectedCharts = new javax.swing.JPanel();
		jPnlBottomTop = new javax.swing.JPanel();
		jPnlButtonsLeft = new javax.swing.JPanel();
		jPnlButtons = new javax.swing.JPanel();
		jBtnLast = new javax.swing.JButton();
		jBtnAdd = new javax.swing.JButton();
		jBtnQuit = new javax.swing.JButton();
		jBtnNext = new javax.swing.JButton();
		jPnlButtonsRight = new javax.swing.JPanel();
		jPnlBottomBottom = new javax.swing.JPanel();
		jPnlBottomRigth = new javax.swing.JPanel();
		jPnlBottomLeft = new javax.swing.JPanel();
		jScrPnlCandidates = new javax.swing.JScrollPane();
		jPnlSelection = new javax.swing.JPanel();

		setAutoscrolls(true);
		setName("Form"); // NOI18N
		setPreferredSize(new java.awt.Dimension(500, 482));
		setLayout(new java.awt.GridLayout(2, 1));

		jPnlFlowCharts.setName("jPnlFlowCharts"); // NOI18N
		jPnlFlowCharts.setPreferredSize(new java.awt.Dimension(500, 241));
		jPnlFlowCharts.setLayout(new java.awt.BorderLayout());

		jPnlTopCenter.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
		jPnlTopCenter.setName("jPnlTopCenter"); // NOI18N

		javax.swing.GroupLayout jPnlTopCenterLayout = new javax.swing.GroupLayout(
				jPnlTopCenter);
		jPnlTopCenter.setLayout(jPnlTopCenterLayout);
		jPnlTopCenterLayout.setHorizontalGroup(jPnlTopCenterLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 657, Short.MAX_VALUE));
		jPnlTopCenterLayout.setVerticalGroup(jPnlTopCenterLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 217, Short.MAX_VALUE));

		jPnlFlowCharts.add(jPnlTopCenter, java.awt.BorderLayout.CENTER);

		jPnlTopTop.setName("jPnlTopTop"); // NOI18N
		jPnlTopTop.setPreferredSize(new java.awt.Dimension(675, 10));

		javax.swing.GroupLayout jPnlTopTopLayout = new javax.swing.GroupLayout(
				jPnlTopTop);
		jPnlTopTop.setLayout(jPnlTopTopLayout);
		jPnlTopTopLayout.setHorizontalGroup(jPnlTopTopLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 681, Short.MAX_VALUE));
		jPnlTopTopLayout.setVerticalGroup(jPnlTopTopLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 10,
				Short.MAX_VALUE));

		jPnlFlowCharts.add(jPnlTopTop, java.awt.BorderLayout.PAGE_START);

		jPnlTopBottom.setName("jPnlTopBottom"); // NOI18N
		jPnlTopBottom.setPreferredSize(new java.awt.Dimension(675, 10));

		javax.swing.GroupLayout jPnlTopBottomLayout = new javax.swing.GroupLayout(
				jPnlTopBottom);
		jPnlTopBottom.setLayout(jPnlTopBottomLayout);
		jPnlTopBottomLayout.setHorizontalGroup(jPnlTopBottomLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 681, Short.MAX_VALUE));
		jPnlTopBottomLayout.setVerticalGroup(jPnlTopBottomLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 10, Short.MAX_VALUE));

		jPnlFlowCharts.add(jPnlTopBottom, java.awt.BorderLayout.PAGE_END);

		jPnlTopRigth.setName("jPnlTopRigth"); // NOI18N
		jPnlTopRigth.setPreferredSize(new java.awt.Dimension(10, 125));

		javax.swing.GroupLayout jPnlTopRigthLayout = new javax.swing.GroupLayout(
				jPnlTopRigth);
		jPnlTopRigth.setLayout(jPnlTopRigthLayout);
		jPnlTopRigthLayout.setHorizontalGroup(jPnlTopRigthLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 10, Short.MAX_VALUE));
		jPnlTopRigthLayout.setVerticalGroup(jPnlTopRigthLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 221, Short.MAX_VALUE));

		jPnlFlowCharts.add(jPnlTopRigth, java.awt.BorderLayout.LINE_END);

		jPnlTopLeft.setName("jPnlTopLeft"); // NOI18N
		jPnlTopLeft.setPreferredSize(new java.awt.Dimension(10, 125));

		javax.swing.GroupLayout jPnlTopLeftLayout = new javax.swing.GroupLayout(
				jPnlTopLeft);
		jPnlTopLeft.setLayout(jPnlTopLeftLayout);
		jPnlTopLeftLayout.setHorizontalGroup(jPnlTopLeftLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 10, Short.MAX_VALUE));
		jPnlTopLeftLayout.setVerticalGroup(jPnlTopLeftLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 221, Short.MAX_VALUE));

		jPnlFlowCharts.add(jPnlTopLeft, java.awt.BorderLayout.LINE_START);

		add(jPnlFlowCharts);

		jPnlSelectedCharts.setName("jPnlSelectedCharts"); // NOI18N
		jPnlSelectedCharts.setPreferredSize(new java.awt.Dimension(500, 185));
		jPnlSelectedCharts.setLayout(new java.awt.BorderLayout());

		jPnlBottomTop.setName("jPnlBottomTop"); // NOI18N
		jPnlBottomTop.setPreferredSize(new java.awt.Dimension(675, 40));
		jPnlBottomTop.setLayout(new javax.swing.BoxLayout(jPnlBottomTop,
				javax.swing.BoxLayout.LINE_AXIS));

		jPnlButtonsLeft.setName("jPnlButtonsLeft"); // NOI18N
		jPnlButtonsLeft.setPreferredSize(new java.awt.Dimension(140, 50));

		javax.swing.GroupLayout jPnlButtonsLeftLayout = new javax.swing.GroupLayout(
				jPnlButtonsLeft);
		jPnlButtonsLeft.setLayout(jPnlButtonsLeftLayout);
		jPnlButtonsLeftLayout.setHorizontalGroup(jPnlButtonsLeftLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 125, Short.MAX_VALUE));
		jPnlButtonsLeftLayout.setVerticalGroup(jPnlButtonsLeftLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 40, Short.MAX_VALUE));

		jPnlBottomTop.add(jPnlButtonsLeft);

		jPnlButtons.setName("jPnlButtons"); // NOI18N

		javax.swing.ActionMap actionMap = org.jdesktop.application.Application
				.getInstance(systole.view.SystoleApp.class).getContext()
				.getActionMap(JPnlImagesFlow.class, this);
		jBtnLast.setAction(actionMap.get("last")); // NOI18N
		org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
				.getInstance(systole.view.SystoleApp.class).getContext()
				.getResourceMap(JPnlImagesFlow.class);
		jBtnLast.setIcon(resourceMap.getIcon("jBtnLast.icon")); // NOI18N
		jBtnLast.setText(resourceMap.getString("jBtnLast.text")); // NOI18N
		jBtnLast.setToolTipText(resourceMap.getString("jBtnLast.toolTipText")); // NOI18N
		jBtnLast.setName("jBtnLast"); // NOI18N
		jBtnLast.setPreferredSize(new java.awt.Dimension(72, 27));

		jBtnAdd.setAction(actionMap.get("addSegment")); // NOI18N
		jBtnAdd.setIcon(resourceMap.getIcon("jBtnAdd.icon")); // NOI18N
		jBtnAdd.setText(resourceMap.getString("jBtnAdd.text")); // NOI18N
		jBtnAdd.setToolTipText(resourceMap.getString("jBtnAdd.toolTipText")); // NOI18N
		jBtnAdd.setName("jBtnAdd"); // NOI18N
		jBtnAdd.setPreferredSize(new java.awt.Dimension(72, 27));

		jBtnQuit.setAction(actionMap.get("removeSegment")); // NOI18N
		jBtnQuit.setIcon(resourceMap.getIcon("jBtnQuit.icon")); // NOI18N
		jBtnQuit.setText(resourceMap.getString("jBtnQuit.text")); // NOI18N
		jBtnQuit.setToolTipText(resourceMap.getString("jBtnQuit.toolTipText")); // NOI18N
		jBtnQuit.setName("jBtnQuit"); // NOI18N
		jBtnQuit.setPreferredSize(new java.awt.Dimension(72, 27));

		jBtnNext.setAction(actionMap.get("next")); // NOI18N
		jBtnNext.setIcon(resourceMap.getIcon("jBtnNext.icon")); // NOI18N
		jBtnNext.setText(resourceMap.getString("jBtnNext.text")); // NOI18N
		jBtnNext.setToolTipText(resourceMap.getString("jBtnNext.toolTipText")); // NOI18N
		jBtnNext.setName("jBtnNext"); // NOI18N

		javax.swing.GroupLayout jPnlButtonsLayout = new javax.swing.GroupLayout(
				jPnlButtons);
		jPnlButtons.setLayout(jPnlButtonsLayout);
		jPnlButtonsLayout
				.setHorizontalGroup(jPnlButtonsLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPnlButtonsLayout
										.createSequentialGroup()
										.addComponent(
												jBtnLast,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												100,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jBtnAdd,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												100,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jBtnQuit,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												100,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jBtnNext,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												100,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));
		jPnlButtonsLayout.setVerticalGroup(jPnlButtonsLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPnlButtonsLayout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jBtnLast,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jBtnAdd,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jBtnQuit,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jBtnNext)));

		jPnlButtonsLayout.linkSize(javax.swing.SwingConstants.VERTICAL,
				new java.awt.Component[] { jBtnAdd, jBtnLast, jBtnNext,
						jBtnQuit });

		jPnlBottomTop.add(jPnlButtons);

		jPnlButtonsRight.setName("jPnlButtonsRight"); // NOI18N
		jPnlButtonsRight.setPreferredSize(new java.awt.Dimension(140, 50));

		javax.swing.GroupLayout jPnlButtonsRightLayout = new javax.swing.GroupLayout(
				jPnlButtonsRight);
		jPnlButtonsRight.setLayout(jPnlButtonsRightLayout);
		jPnlButtonsRightLayout.setHorizontalGroup(jPnlButtonsRightLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 125, Short.MAX_VALUE));
		jPnlButtonsRightLayout.setVerticalGroup(jPnlButtonsRightLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 40, Short.MAX_VALUE));

		jPnlBottomTop.add(jPnlButtonsRight);

		jPnlSelectedCharts.add(jPnlBottomTop, java.awt.BorderLayout.PAGE_START);

		jPnlBottomBottom.setName("jPnlBottomBottom"); // NOI18N
		jPnlBottomBottom.setPreferredSize(new java.awt.Dimension(675, 10));

		javax.swing.GroupLayout jPnlBottomBottomLayout = new javax.swing.GroupLayout(
				jPnlBottomBottom);
		jPnlBottomBottom.setLayout(jPnlBottomBottomLayout);
		jPnlBottomBottomLayout.setHorizontalGroup(jPnlBottomBottomLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 681, Short.MAX_VALUE));
		jPnlBottomBottomLayout.setVerticalGroup(jPnlBottomBottomLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 10, Short.MAX_VALUE));

		jPnlSelectedCharts
				.add(jPnlBottomBottom, java.awt.BorderLayout.PAGE_END);

		jPnlBottomRigth.setName("jPnlBottomRigth"); // NOI18N
		jPnlBottomRigth.setPreferredSize(new java.awt.Dimension(10, 115));

		javax.swing.GroupLayout jPnlBottomRigthLayout = new javax.swing.GroupLayout(
				jPnlBottomRigth);
		jPnlBottomRigth.setLayout(jPnlBottomRigthLayout);
		jPnlBottomRigthLayout.setHorizontalGroup(jPnlBottomRigthLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 10, Short.MAX_VALUE));
		jPnlBottomRigthLayout.setVerticalGroup(jPnlBottomRigthLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 191, Short.MAX_VALUE));

		jPnlSelectedCharts.add(jPnlBottomRigth, java.awt.BorderLayout.LINE_END);

		jPnlBottomLeft.setName("jPnlBottomLeft"); // NOI18N
		jPnlBottomLeft.setPreferredSize(new java.awt.Dimension(10, 115));

		javax.swing.GroupLayout jPnlBottomLeftLayout = new javax.swing.GroupLayout(
				jPnlBottomLeft);
		jPnlBottomLeft.setLayout(jPnlBottomLeftLayout);
		jPnlBottomLeftLayout.setHorizontalGroup(jPnlBottomLeftLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 10, Short.MAX_VALUE));
		jPnlBottomLeftLayout.setVerticalGroup(jPnlBottomLeftLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 191, Short.MAX_VALUE));

		jPnlSelectedCharts
				.add(jPnlBottomLeft, java.awt.BorderLayout.LINE_START);

		jScrPnlCandidates.setBackground(resourceMap
				.getColor("jScrPnlCandidates.background")); // NOI18N
		jScrPnlCandidates.setBorder(null);
		jScrPnlCandidates
				.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		jScrPnlCandidates.setName("jScrPnlCandidates"); // NOI18N

		jPnlSelection.setName("jPnlSelection"); // NOI18N
		jPnlSelection.setLayout(new java.awt.GridBagLayout());
		jScrPnlCandidates.setViewportView(jPnlSelection);

		jPnlSelectedCharts.add(jScrPnlCandidates, java.awt.BorderLayout.CENTER);

		add(jPnlSelectedCharts);
	}// </editor-fold>//GEN-END:initComponents
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jBtnAdd;
	private javax.swing.JButton jBtnLast;
	private javax.swing.JButton jBtnNext;
	private javax.swing.JButton jBtnQuit;
	private javax.swing.JPanel jPnlBottomBottom;
	private javax.swing.JPanel jPnlBottomLeft;
	private javax.swing.JPanel jPnlBottomRigth;
	private javax.swing.JPanel jPnlBottomTop;
	private javax.swing.JPanel jPnlButtons;
	private javax.swing.JPanel jPnlButtonsLeft;
	private javax.swing.JPanel jPnlButtonsRight;
	private javax.swing.JPanel jPnlFlowCharts;
	private javax.swing.JPanel jPnlSelectedCharts;
	private javax.swing.JPanel jPnlSelection;
	private javax.swing.JPanel jPnlTopBottom;
	private javax.swing.JPanel jPnlTopCenter;
	private javax.swing.JPanel jPnlTopLeft;
	private javax.swing.JPanel jPnlTopRigth;
	private javax.swing.JPanel jPnlTopTop;
	private javax.swing.JScrollPane jScrPnlCandidates;

	// End of variables declaration//GEN-END:variables
    /**
     * @param imageFlow
     */
    public void addFlowCharts(ImageFlow imageFlow) {
        this.imageFlow = imageFlow;
        this.jPnlTopCenter.removeAll();
        this.jPnlTopCenter.setLayout(new StackLayout());
        this.jPnlTopCenter.add(new GradientPanel(), StackLayout.BOTTOM);
        this.jPnlTopCenter.add(this.imageFlow, StackLayout.TOP);
        this.jPnlTopCenter.revalidate();
    }

    /**
     * 
     */
    @Action
    public void last() {
        if (this.imageFlow != null) {
            this.imageFlow.goBack();
        }
    }

    /**
     * 
     */
    @Action
    public void next() {
        if (this.imageFlow != null) {
            this.imageFlow.goNext();
        }
    }

    /**
     * 
     */
    @Action
    public void addSegment() {

        if ((this.imageFlow == null) || (this.imageFlow.getSelectedIndex() < 0)) {
            return;
        }

        int item = imageFlow.getSelectedIndex();
        this.addSegment(item);
    }

    private void addSegment(int item) {
        if (!this.items.containsKey(item)) {

            if ((x == 0) || (y == 0)) {
                x = this.jPnlSelection.getWidth() / 4;
                y = this.jPnlSelection.getHeight() - 15;
            }
            Dimension dim = new Dimension(x, y);
            XYChart chart;
            String name;
            ChartPanel pnl;
            Segment currentSegment = this.analysisControl.getAnalysis().getAllSegments().getSegments().elementAt(
                    item);
            name = "Segmento: " + (item);
            chart = new XYChart(name);
            chart.addSeries(name, currentSegment.toDoubleArray(), 1);
            chart.setShowLegend(false);
            chart.setShowTitle(false);
            pnl = chart.plot();
            pnl.setMouseZoomable(false);
            pnl.setPopupMenu(null);
            JPnlItemFlow img = new JPnlItemFlow(item, pnl,
                    this);
            img.setPreferredSize(dim);
            img.revalidate();
            this.jPnlSelection.add(img);
            this.jPnlSelection.revalidate();
            this.items.put(item, img);
            this.analysisControl.getSelectionModel().addSegment(item);
        }
    }

    /**
     * 
     */
    public void removeAllPanels() {
        this.items.clear();
        this.jPnlSelection.removeAll();
        this.jPnlSelection.setLayout(new GridBagLayout());
        this.jPnlSelection.setBackground(this.jPnlSelection.getBackground());
        this.jPnlSelection.repaint();
        this.jPnlSelection.revalidate();
        this.jScrPnlCandidates.revalidate();
    }

    @Action
    public void removeSegment() {
        if (this.selectedToDelete != null) {
            this.removeSegment(this.selectedToDelete);
            this.selectedToDelete = null;
        }
    }

    /**
     * @param pnlToRemove
     */
    public void removeSegment(JPnlItemFlow pnlToRemove) {
        if (this.items.containsKey(pnlToRemove.getSegmentIndex())) {
            this.items.remove(pnlToRemove.getSegmentIndex());
            this.jPnlSelection.remove(pnlToRemove);
            this.jPnlSelection.setLayout(new GridBagLayout());
            this.jPnlSelection.setBackground(this.jPnlSelection.getBackground());
            this.jPnlSelection.repaint();
            this.jPnlSelection.revalidate();
            this.jScrPnlCandidates.revalidate();
            this.analysisControl.getSelectionModel().removeSegment(pnlToRemove.getSegmentIndex());
        }
    }

    public void update(Observable o, Object arg) {
        if (arg == null) {
            this.removeAllPanels();
            SelectionModel model = (SelectionModel) o;
            if (!model.isSelectionEmpty()) {
                Iterator<SelectionItem> itemsIt = model.getCurrentSegments().iterator();
                while (itemsIt.hasNext()) {
                    SelectionItem selectItem = itemsIt.next();
                    if (selectItem.isSelected()) {
                        this.addSegment(selectItem.getIndexSegement());
                    }
                }
            }
        } else {
            SelectionItem selectItem = (SelectionItem) arg;
            if ((selectItem.isSelected())) {
                if (!this.items.containsKey(selectItem.getIndexSegement())) {
                    this.addSegment(selectItem.getIndexSegement());
                }
            } else {
                if (this.items.containsKey(selectItem.getIndexSegement())) {
                    this.removeSegment(this.items.get(selectItem.getIndexSegement()));
                }
            }
        }
    }

    /**
     * @param pnlToCorrelation
     */
    public void applyCorrelation(JPnlItemFlow pnlToCorrelation) {
        Segment segment = this.analysisControl.getAnalysis().getAllSegments().getSegments().get(
                pnlToCorrelation.getSegmentIndex());
        if (segment != null) {
            this.parent.applyCorrelation(segment);
        }
    }

    /**
     * @param pnlToRemove
     */
    public void setAsSelectedToDelete(JPnlItemFlow pnlToRemove) {
        if (this.selectedToDelete != null) {
            this.selectedToDelete.unclick();
        }
        this.selectedToDelete = pnlToRemove;
    }
}
